package com.xxgc.vfc.modules.provider.bd.service;

import com.xxgc.vfc.api.dto.SchoolsDTO;
import com.xxgc.vfc.api.service.ISchoolService;
import com.xxgc.vfc.api.vo.SchoolsVO;
import com.xxgc.vfc.common.redis.RedisUtil;
import com.xxgc.vfc.modules.provider.bd.entity.Schools;
import com.xxgc.vfc.modules.provider.bd.mapper.SchoolsMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author: SJY
 * @Date: 2023/11/7 09:39:16
 * 提供学校的基础服务
 */
@Slf4j
@DubboService
public class SchoolServiceImpl implements ISchoolService {

    @Autowired
    private SchoolsMapper schoolsMapper;

    @Autowired
    private RedisUtil redisUtil;

    private String KeyPrefix = "select_school_";

    @Override
    public boolean addSchool(SchoolsVO schoolsVO) {
        log.info("-----------------------------------------");
        //VO转Entity
        Schools schools = new Schools();
        BeanUtils.copyProperties(schoolsVO,schools);
        schoolsMapper.insert(schools);
        log.info(schoolsVO.toString());
        return true;
    }

    @Override
    public SchoolsDTO getSchoolById(Integer id) {
        //1、看缓存有没有
        String key = KeyPrefix + id;
        if(redisUtil.hasKey(key)){
            SchoolsDTO schoolsDTO = (SchoolsDTO) redisUtil.get(key);
            log.info("-------------内存-------------");
            return schoolsDTO;
        }else{
            Schools schools = schoolsMapper.selectById(id);
            //如果查询为空
            SchoolsDTO schoolsDTO = new SchoolsDTO();
            BeanUtils.copyProperties(schools,schoolsDTO);
            log.info("------------数据库--------------");
            redisUtil.set(key,schoolsDTO,60);
            return schoolsDTO;
        }
    }

}
